﻿using System.Web.Mvc;
using Amba.KeyValueStorage.Services;
using Orchard.Environment.Extensions;
using Orchard.Themes;

namespace Amba.KeyValueStorage.Controllers
{
    [Themed(false)]    
    public class KeyValueController: Controller
    {
        private readonly IKeyValueStorageService _keyValueRepo;

        public KeyValueController(IKeyValueStorageService keyValueRepo)
        {
            _keyValueRepo = keyValueRepo;
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Get(string key)
        {
            return Content(_keyValueRepo.Get(key));
        }

        public ActionResult AntiValidationToken()
        {
            Response.ContentType = "text/plain";
            return View();
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Set(string value, string key = "")
        {
            if (string.IsNullOrWhiteSpace(key))
                key = _keyValueRepo.GenerateKey();
            _keyValueRepo.Set(key, value);
            return Content(key);
        }

        public ActionResult Test()
        {
            return View();
        }
    }
}